﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using XmlDomain.Properties;
using System.Xml.Linq;
namespace XmlDomain
{
    public class Repository:IRepository
    {


        private static IQueryable<IContact> GetContacts()
        {
            var doc = XDocument.Parse(Resources.Contacts);
            
            // ReSharper disable PossibleNullReferenceException
            var contactList = new List<IContact>();
            foreach (var element in doc.Element("rows").Elements("row"))
            {
               
                var id = int.Parse(element.Attribute("ContactID").Value);

                var firstName = element.Attribute("FirstName").Value;

                var lastName = element.Attribute("LastName").Value;
                
                IContact contact = new Contact();
                contact.FirstName = firstName;
                contact.LastName = lastName;
                contact.Id = id;

                contactList.Add(contact);
            }

            return contactList.AsQueryable();

        }


        #region IRepository Members

        public IQueryable<IContact> Contacts
        {
            get 
            {
                return GetContacts();
            }
        }

        #endregion
    }
}
